package com.menu.Controller;


import com.menu.Pojo.AddressVO;
import com.menu.Pojo.SecurityUser;
import com.menu.Service.AddressService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(value = "收货地址相关", tags = {"收货地址相关的api接口"})
@RequestMapping("/Address")
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private CommonUtil commonUtil;

    @ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
    @PostMapping("/list")
    public ResponseResult list(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser) {
        List<AddressVO> list = addressService.queryAll(securityUser.getUserId());
        return ResponseResult.success(list);
    }

    @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
    @PostMapping("/add")
    public ResponseResult add(AddressVO addressBO,
                              @ApiIgnore @AuthenticationPrincipal SecurityUser securityUser) {
        ResponseResult checkRes = checkAddress(addressBO);
        if (checkRes.getCode() != 200) {
            return checkRes;
        }
        addressBO.setUserId(securityUser.getUserId());
        return addressService.addNewUserAddress(addressBO);
    }

    private ResponseResult checkAddress(AddressVO addressBO) {
        String receiver = addressBO.getReceiver();
        if (!commonUtil.verifyStirng(receiver)) {
            return ResponseResult.error("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return  ResponseResult.error("收货人姓名不能太长");
        }
        String mobile = addressBO.getMobile();
        if (!commonUtil.verifyStirng(mobile)) {
            return ResponseResult.error("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return  ResponseResult.error("收货人手机号长度不正确");
        }
        boolean isMobileOk = CommonUtil.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return  ResponseResult.error("收货人手机号格式不正确");
        }
        if (addressBO.getIsDefault() == null ||
                !(addressBO.getIsDefault() == 0 ||
                        addressBO.getIsDefault() == 1)){
            addressBO.setIsDefault(0);
        }
        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (!commonUtil.verifyStirng(province) ||
                !commonUtil.verifyStirng(city) ||
                !commonUtil.verifyStirng(district) ||
                !commonUtil.verifyStirng(detail)) {
            return  ResponseResult.error("收货地址信息不能为空");
        }
        return  ResponseResult.success("请求成功！");
    }

    @ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")
    @PostMapping("/update")
    public ResponseResult update(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                 AddressVO addressBO) {
        if (addressBO.getAddressId() == null) {
            return ResponseResult.error("修改地址错误：addressId不能为空");
        }
        ResponseResult checkRes = checkAddress(addressBO);
        if (checkRes.getCode() != 200) {
            return checkRes;
        }
        addressBO.setUserId(securityUser.getUserId());
        return addressService.updateUserAddress(addressBO);
    }

    @ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")
    @PostMapping("/delete")
    public ResponseResult delete(
           @ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
            @RequestParam Integer addressId) {
        if (addressId == null){
            return ResponseResult.error("参数有误！");
        }
        return  addressService.deleteUserAddress(securityUser.getUserId(), addressId);
    }

    @ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")
    @PostMapping("/setDefalut")
    public ResponseResult setDefalut(
           @ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
            @RequestParam Integer addressId) {
        if (addressId == null) {
            return ResponseResult.error("参数有误！");
        }
        return  addressService.updateUserAddressToBeDefault(securityUser.getUserId(), addressId);
    }
}
